package fregat.base.util;

import java.util.Hashtable;

public class StopWatch {
  private static Hashtable categories;
  private static String MAIN = "main";
  private static boolean enabled = true;

  public static void setEnabled(boolean b) {
    enabled = b;
  }

  public static boolean isEnabled() {
    return enabled;
  }

  public static void resetTime() {
    resetTime(MAIN);
  }

  public static long takeTime() {
    return takeTime(MAIN);
  }

  public static void displayTime(String prefix) {
    displayTime(MAIN, prefix);
  }

  public static void resetTime(String category) {
    if (categories == null) {
      categories = new Hashtable();
    }
    categories.put(category, new Long(System.currentTimeMillis()));
  }

  public static long takeTime(String category) {
    long lngTime = 0;
    try {
      lngTime = System.currentTimeMillis() - ((Long) categories.get(category)).longValue();
    } catch (Exception ex) {
    }
    return lngTime;
  }

  public static void displayTime(String category, String prefix) {
    String str = prefix + takeTime(category) + "[ms]";
    if (enabled) System.out.println(str);
  }

}
